package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.entity.ShopAvgEvaluationEntity;
import com.yuanfeng.business.mapper.ShopAvgEvaluationMapper;
import com.yuanfeng.business.service.ShopAvgEvaluationService;
import com.yuanfeng.commoms.dto.shop.ShopEvaluationDTO;
import com.yuanfeng.commoms.dto.shop.ShopPercentDTO;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @ClassName ShopAvgEvaluationServiceImpl
 * @Description 店铺评分发货速度平均表
 * @Author cyp
 * @Date 2023-05-26 11:18:16
 * @Version 1.0
 **/
@Service
public class ShopAvgEvaluationServiceImpl extends ServiceImpl<ShopAvgEvaluationMapper, ShopAvgEvaluationEntity>
        implements ShopAvgEvaluationService {

    @Override
    public ResponseResult shopScore(Map<Object, Object> map) {
        ResponseResult rr = new ResponseResult();
        try {
            Integer shopId = Integer.parseInt(map.get("shopId") + "");
            ShopEvaluationDTO see = this.baseMapper.shopScore(shopId);
            if(see != null){
                Double servicecreditScore = see.getAvgServicecredit();
                Double desccreditScore = see.getAvgDesccredit();
                Double deliverycreditScore = see.getAvgDeliverycredit();
                if(String.valueOf(servicecreditScore) == null || see.getAvgServicecredit() == 0){
                    see.setEvaluationServicecredit(5);
                }else{
                    see.setEvaluationServicecredit(see.getAvgServicecredit());
                }
                if(String.valueOf(desccreditScore) == null || see.getAvgDesccredit() == 0){
                    see.setEvaluationDesccredit(5);
                }else{
                    see.setEvaluationDesccredit(see.getAvgDesccredit());
                }
                if(String.valueOf(deliverycreditScore) == null || see.getAvgDeliverycredit() == 0){
                    see.setEvaluationDeliverycredit(5);
                }else{
                    see.setEvaluationDeliverycredit(see.getAvgDeliverycredit());
                }
                ShopPercentDTO sp = this.baseMapper.queryShopPercent(servicecreditScore,desccreditScore,deliverycreditScore);
                String servicecreditNum = String.format("%.2f",(Double.parseDouble(sp.getServicecreditPercent()) * 100)) + "%";
                String desccreditNum = String.format("%.2f",(Double.parseDouble(sp.getDesccreditPercent()) * 100)) + "%";
                String deliverycreditNum = String.format("%.2f",(Double.parseDouble(sp.getDeliverycreditPercent()) * 100)) + "%";
                sp.setDeliverycreditPercent(deliverycreditNum);
                sp.setDesccreditPercent(desccreditNum);
                sp.setServicecreditPercent(servicecreditNum);
                see.setShopPercentDTO(sp);
                BigDecimal shippingTime = this.baseMapper.queryShippingTime(shopId);
                BigDecimal receivingTime = shippingTime.setScale(0,BigDecimal.ROUND_HALF_UP);
                see.setAvgShippingTime(receivingTime);
                rr.setCode(1);
                rr.setData(see);
            }else{
                ShopEvaluationDTO sees = new ShopEvaluationDTO();
                ShopPercentDTO sp = new ShopPercentDTO();
                sp.setDeliverycreditPercent("100%");
                sp.setDesccreditPercent("100%");
                sp.setServicecreditPercent("100%");
                sees.setShopPercentDTO(sp);
                sees.setEvaluationDesccredit(5);
                sees.setEvaluationServicecredit(5);
                sees.setEvaluationDeliverycredit(5);
                rr.setCode(1);
                rr.setData(sees);
            }
        } catch (Exception e) {
            rr.setCode(500);
        }
        return rr;
    }
}
